package com.example.education.service.impl;

import com.example.education.exception.ParamException;
import com.example.education.dao.ReplyMapper;
import com.example.education.model.Reply;
import com.example.education.service.ReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ReplyServiceImpl implements ReplyService {

    @Autowired
    ReplyMapper replyMapper;

    @Override
    public boolean insertReply(Reply reply) throws ParamException {
        if(reply==null){
            throw new ParamException("参数为空");
        }else if (reply.getContent()==null){
            throw new ParamException("Content为空");
        }else if(reply.getUserId()==null){
            throw new ParamException("UserId为空");
        }else if (reply.getReplyCate()==null){
            throw new ParamException("ReplyCate为空");
        }else if (reply.getReplypId()==null){
            throw new ParamException("ReplyPId为空");
        }
        reply.setCreatTime(new Date());
        Integer result =  replyMapper.insertReply(reply);
        if(result>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean deleteReply(Integer replyId) throws ParamException {
        if(replyId==null){
            throw new ParamException("参数为空");
        }
        Integer result =  replyMapper.deleteReply(replyId);
        if(result>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean likeReply(Integer replyId) throws ParamException {
        if(replyId==null){
            throw new ParamException("参数为空");
        }
        Reply reply = getReplyById(replyId);
        Integer likeNum = reply.getLikeNum();
        Integer result =  replyMapper.likeReply(replyId,likeNum);
        if(result>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Reply getReplyById(Integer replyId) throws ParamException {
        if(replyId==null){
            throw new ParamException("参数为空");
        }
        return replyMapper.getReplyById(replyId);
    }

    @Override
    public List<Reply> getRelyByCateAndPid(Integer cate, Integer pid) throws ParamException {
        if(cate==null){
            throw new ParamException("cate为空");
        }else if(pid==null){
            throw new ParamException("pid为空");
        }
        return replyMapper.getRelyByCateAndPid(cate,pid);
    }
}
